package com.yxh.lib_base.ext

import android.util.SparseIntArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.yxh.lib_base.ext.viewbinding.ext.inflateMethodParent
import java.lang.reflect.ParameterizedType

/**
 *@创建者：yuan_xh
 *@创建时间：2021/3/30 9:49
 *@描述：
 */

class ViewBindingHolder<VB : ViewBinding>(val vb: VB, view: View) :  RecyclerView.ViewHolder(view)

open class MBaseViewHolder(view: View) : BaseViewHolder(view)

abstract class MBaseQuickAdapter<T, K : BaseViewHolder>(data: MutableList<T>? = mutableListOf()):
    BaseQuickAdapter<T, K>(0,data)

/**
 * viewbinding
 * */
class VBViewHolder<VB : ViewBinding>(val vb: VB, view: View) : MBaseViewHolder(view)

abstract class BaseViewBindingAdapter<VB : ViewBinding, T>(data: MutableList<T>? = mutableListOf()) :
        MBaseQuickAdapter<T, VBViewHolder<VB>>( data) {
    private val vbClass by lazy(LazyThreadSafetyMode.NONE ) { (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB> }
    private val layoutInflater by lazy(LazyThreadSafetyMode.NONE ) {  vbClass.inflateMethodParent()}

    //重写返回自定义 ViewHolder
    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): VBViewHolder<VB> {
        val mBinding =
            layoutInflater.invoke(null, LayoutInflater.from(parent.context), parent, false) as VB
        return VBViewHolder(mBinding, mBinding.root)
    }
}

abstract class BaseMultiItemAdapter<T : MultiItemEntity, VH : MBaseViewHolder>(data: MutableList<T>? = null)
    : BaseQuickAdapter<T, VH>(0, data) {

    private val layouts: SparseIntArray by lazy(LazyThreadSafetyMode.NONE) { SparseIntArray() }

    override fun getDefItemViewType(position: Int): Int {
        return data[position].itemType
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): VH {
        val layoutResId = layouts.get(viewType)
        require(layoutResId != 0) { "ViewType: $viewType found layoutResId，please use addItemType() first!" }
        return createBaseViewHolder(parent, layoutResId)
    }

    /**
     * 调用此方法，设置多布局
     * @param type Int
     * @param layoutResId Int
     */
    protected fun addItemType(type: Int, @LayoutRes layoutResId: Int) {
        layouts.put(type, layoutResId)
    }
}

/**
 *点击事件 -防止充点
 * */
private var lastClickTime = 0L
fun <VB : ViewDataBinding, T> BaseViewBindingAdapter<VB,T>.setOnItemClickListenerNb(interval: Long = 500,call:(binding:VB?,item:T,position:Int)->Unit){
    setOnItemClickListener { adapter, view, position ->
        val currentTime = System.currentTimeMillis()
        if (lastClickTime != 0L && (currentTime - lastClickTime < interval)) {
            return@setOnItemClickListener
        }
        lastClickTime = currentTime
        val item = getItem(position)!!
        val binding = DataBindingUtil.findBinding<VB>(view)
        call.invoke(binding,item,position)
    }
}



